package com.stylefeng.guns.modular.small.controller;

import com.stylefeng.guns.modular.small.dto.OrderParamDTO;
import com.stylefeng.guns.modular.small.util.ResponseResultDTO;
import com.stylefeng.guns.modular.system.service.IConsumerOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@Slf4j
@RequestMapping("/wx/pay")
public class WxPayController {


    @Autowired
    private IConsumerOrderService consumerOrderService;


    @RequestMapping({"/testPay"})
    public String testPay() {
        return "/payPage.html";
    }

    /**
     * @api {POST} /wx/pay/unifiedorder  JSAPI支付 小程序支付
     * @apiVersion 1.1.0
     * @apiName createOrder
     * @apiGroup ConsumerOrder
     * @apiParam {Number} appointmentTime 预约的时间Long.
     * @apiParam {Number} deliveryFee  配送费用.
     * @apiParam {String} phone 手机号.
     * @apiParam {Number} totalFee  总金额.
     * @apiParam {Number} businessType 业务类型   服务业务的id.
     * @apiParam {Number} addressId  服务地址的ID.
     * @apiParam {Number} type 标识 1上门服务  0是上店服务.
     * @apiParam {String} remark  备注.
     * @apiParam {Number} consumerId 消费者id.
     * @apiParam {Number} artificerId  技师id.
     * @apiSuccess {String}   return_code      返回编码.
     * @apiSuccess {String}   return_msg     返回信息.
     * @apiSuccess {String}   result_code   结果编码.
     * @apiSuccess {String}   err_code      错误码.
     * @apiSuccessExample Success-Response:
     * HTTP/1.1 200 OK
     * {
     * "data":{
     * "nonce_str":"4weeTM8DRC2p1WZq",
     * "appid":"wx224e8b702efc0e2b",
     * "sign":"E6DE2615617A8D7713B0391CC13F4B0C",
     * "err_code":"PARAM_ERROR",
     * "return_msg":"OK",
     * "result_code":"FAIL",
     * "err_code_des":"appid和openid不匹配",
     * "mch_id":"",
     * "return_code":"SUCCESS"
     * },
     * "msg":"OK",
     * "status":"0000",
     * "userType":""
     * }
     */
    @RequestMapping("/unifiedorder")
    @ResponseBody
    public ResponseResultDTO createOrder(HttpServletRequest request, OrderParamDTO orderParamDTO) {
        return consumerOrderService.createOrder(request, orderParamDTO);
    }

    /**
     * <p>通知地址</p>
     *
     * @param request
     * @param attach
     * @return
     * @des 异步接收微信支付结果通知的回调地址，通知url必须为外网可访问的url，不能携带参数。
     */
    @RequestMapping("/callback")
    @ResponseBody
    public ResponseResultDTO callback(HttpServletRequest request, String attach) {
        log.info("异步接收微信支付结果通知的回调地址,attach:{}", attach);
        return consumerOrderService.orderqueryAndUpdateOrder(request, attach);
    }

    /**
     * <p>申请退款</p>
     *
     * @param orderNo
     * @return
     * @des 发起退款申请
     */
    @RequestMapping("/refund")
    @ResponseBody
    public ResponseResultDTO refund(String orderNo) {
        log.info("发起退款申请,申请退款的订单号:{}", orderNo);
        return consumerOrderService.refund(orderNo);
    }

    /**
     * <p>查询退款</p>
     *
     * @param orderNo
     * @return
     * @des 查询退款
     */
    @RequestMapping("/refundquery")
    @ResponseBody
    public ResponseResultDTO refundQuery(String orderNo) {
        log.info("查询退款,查询退款的订单号:{}", orderNo);
        return consumerOrderService.refundQuery(orderNo);
    }

}
